# Maintainer: Felix Yan <felixonmars@archlinux.org>

_hkgname=hiedb
pkgname=haskell-hiedb
pkgver=0.4.2.0
pkgrel=17
pkgdesc="Generates a references DB from .hie files"
url="https://github.com/wz1000/HieDb"
license=("BSD")
arch=('loong64' 'x86_64')
depends=('ghc-libs' 'haskell-algebraic-graphs' 'haskell-ansi-terminal' 'haskell-extra' 'haskell-ghc'
         'haskell-ghc-paths' 'haskell-hie-compat' 'haskell-lucid' 'haskell-optparse-applicative'
	 'haskell-sqlite-simple' 'haskell-terminal-size')
makedepends=('ghc' 'uusi' 'haskell-hspec' 'haskell-temporary')
source=("https://hackage.haskell.org/packages/archive/$_hkgname/$pkgver/$_hkgname-$pkgver.tar.gz")
sha512sums=('77b7b45ed63e72c8e93a7945f76449e3350f3bf4f6e6c5e6e526e44d852c9fcc20019fc0ac734e367722cf212ba99518ee7a1f6fa28decb4f236c93ad3f7dde8')

prepare() {
  cd hiedb-$pkgver
  sed -i 's/callProcess hc args/callProcess hc (["-dynamic"] ++ args)/' test/Main.hs

  # algebraic-graphs 0.7
  sed -i 's/dfs vs m/dfs m vs/' src/HieDb/Query.hs

  uusi -u algebraic-graphs $_hkgname.cabal
}

build() {
  cd hiedb-$pkgver

  runhaskell Setup configure -O --enable-shared --enable-executable-dynamic --disable-library-vanilla \
    --prefix=/usr --docdir=/usr/share/doc/$pkgname --datasubdir=$pkgname --enable-tests \
    --dynlibdir=/usr/lib --libsubdir=\$compiler/site-local/\$pkgid --ghc-option=-fllvm \
    --ghc-option=-optl-Wl\,-z\,relro\,-z\,now \
    --ghc-option='-pie'

  runhaskell Setup build $MAKEFLAGS
  runhaskell Setup register --gen-script
  runhaskell Setup unregister --gen-script
  sed -i -r -e "s|ghc-pkg.*update[^ ]* |&'--force' |" register.sh
  sed -i -r -e "s|ghc-pkg.*unregister[^ ]* |&'--force' |" unregister.sh
}

check() {
  cd hiedb-$pkgver
  PATH="$PWD/dist/build/hiedb:$PATH" LD_LIBRARY_PATH="$PWD/dist/build" runhaskell Setup test --show-details=direct
}

package() {
  cd hiedb-$pkgver

  install -D -m744 register.sh "$pkgdir"/usr/share/haskell/register/$pkgname.sh
  install -D -m744 unregister.sh "$pkgdir"/usr/share/haskell/unregister/$pkgname.sh
  runhaskell Setup copy --destdir="$pkgdir"
  install -D -m644 LICENSE -t "$pkgdir"/usr/share/licenses/$pkgname/
  rm -f "$pkgdir"/usr/share/doc/$pkgname/LICENSE
}
